<template>
	<view class="login">
		<button @click="wxLogin">微信登录</button>
	</view>
</template>

<script>
export default {
	data() {
		return {}
	},
	methods: {
		wxLogin() {
			uni.getUserInfo({
				desc: '登录的数据',
				success(ures) {
					const { encryptedData, iv } = ures
					uni.login({
						success(lres) {
							const { code } = lres
							uni.request({
								url: 'http://127.0.0.1:8001/app/user/login/mini',
								method: 'POST',
								data: {
									code,
									encryptedData,
									iv,
								},
								success({ data }) {
									if (code !== 1000) {
										uni.showToast({
											title: data.message,
											icon: 'none',
										})
										// return;
									}
									uni.showToast({
										title: data.message,
									})
									console.log('登入成功', data)
									//存储token
									uni.setStorageSync('token', data.data.token)
									// 存储用户登录状态
									uni.setStorageSync('isAuthenticated', true)
									//跳转页面
									uni.navigateBack()
								},
							})
						},
					})
				},
			})
		},
	},
}
</script>

<style lang="scss"></style>
